package com.platform.hjt.erp.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.snaker.engine.SnakerEngine;
import org.snaker.engine.access.QueryFilter;
import org.snaker.engine.entity.HistoryOrder;
import org.snaker.engine.entity.WorkItem;
import org.snaker.engine.model.TaskModel.TaskType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.platform.hjt.base.controller.BaseController;
import com.platform.hjt.base.controller.ReturnBody;
import com.platform.hjt.erp.model.ErpOrderInfo;
import com.platform.hjt.erp.service.ErpOrderInfoService;
import com.platform.hjt.sys.service.SysCompanyInfoService;

@Controller
@RequestMapping(value="/erpOrderInfo")
public class ErpOrderInfoController extends BaseController{

	@Resource
	private ErpOrderInfoService erpOrderInfoService;
	@Resource
	private SysCompanyInfoService sysCompanyInfoService;
	@Resource
	private SnakerEngine engine;
	
	@RequestMapping(value="/erpOrderInfo_list.html")
	public String erpOrderInfoListPage(Model model){
		model.addAttribute("curCompany", sysCompanyInfoService.getSysCompanyInfo("sysCompanyInfoId", getSessionSysCompanyInfo().getSysCompanyInfoId()));
		return "page/admin/erpOrderInfo/erpOrderInfo_list";
	}
	
	@RequestMapping(value="/erpOrderInfo_list.do")
	@ResponseBody
	public org.snaker.engine.access.Page<WorkItem> erpOrderInfoListDo(org.snaker.engine.access.Page<WorkItem> page ){
		
		String[] assignees = {getSessionUser().getSysUserInfoId()};
		engine.query()
				.getWorkItems(page, new QueryFilter()
				.setOperators(assignees)
				.setTaskType(TaskType.Major.ordinal()));
		return page;
	}
	
	@RequestMapping(value="/erpOrderInfo_handle_list.html")
	public String erpOrderInfoHandleList(Model model){
		model.addAttribute("curCompany", sysCompanyInfoService.getSysCompanyInfo("sysCompanyInfoId", getSessionSysCompanyInfo().getSysCompanyInfoId()));
		return "page/admin/erpOrderInfo/erpOrderInfo_handle_list";
	}
	
	@RequestMapping(value="/erpOrderInfo_handle_list.do")
	@ResponseBody
	public org.snaker.engine.access.Page<HistoryOrder> erpOrderInfoHandleListDo(org.snaker.engine.access.Page<HistoryOrder> page){
		
		String[] assignees = {getSessionUser().getSysUserInfoId()};
		engine.query().getHistoryOrders(page,  new QueryFilter()
				.setOperators(assignees)
				.setTaskType(TaskType.Major.ordinal()));
		return page;
	}
	
	
	@RequestMapping(value="/erpOrderInfo_edit.html")
	public String erpOrderInfoEditPage(HttpServletRequest request, Model model, String id){
		if(StringUtils.isNotBlank(id)){
			model.addAttribute("erpOrderInfo", erpOrderInfoService.getErpOrderInfo("String", id));
		}
		
		return "page/admin/erpOrderInfo/erpOrderInfo_edit";
	}
	
	@RequestMapping(value="/erpOrderInfo_detail_{id}.html")
	public String erpOrderInfoDetailPage(Model model, @PathVariable String id){
		model.addAttribute("erpOrderInfo", erpOrderInfoService.getErpOrderInfo("String", id));
		return "page/admin/erpOrderInfo/erpOrderInfo_detail";
	}
	
	@RequestMapping(value="/erpOrderInfo_edit.do")
	@ResponseBody
	public ReturnBody erpOrderInfoEditDo(@Valid @ModelAttribute("erpOrderInfo") ErpOrderInfo erpOrderInfo, BindingResult result){
		if(result.hasErrors()){
			return new ReturnBody(false, result.getAllErrors().get(0).getDefaultMessage());
		}
		return erpOrderInfoService.saveErpOrderInfo(erpOrderInfo);
	}
	
	@RequestMapping(value="/erpOrderInfo_del_{id}.do")
	@ResponseBody
	public ReturnBody erpOrderInfoDelDo(@PathVariable String id){
		return erpOrderInfoService.delErpOrderInfo(new ErpOrderInfo(id));
	}
	
	
}
